var express = require('express');
var querydb = require('../utils/querydb');
var router = express.Router(); //获取路由对象

/* 
 * 添加歌曲
 */
router.post("/add", (req, res) => {
    
    const username = req.body.username; //接收表单参数
    const age = req.body.age;
    const sex = req.body.sex;
    const birthplace = req.body.birthplace;
    const tel = req.body.tel;
    const education = req.body.education;
    const politics = req.body.politics;
    const graduate = req.body.graduate;
    const job = req.body.job;

    const sql = 'INSERT INTO y_student (name,age,sex,birthplace,tel,education,politics,graduate,job) VALUES (?,?,?,?,?,?,?,?,?)'
    const params = [username, age, sex, birthplace, tel,education,politics,graduate,job];

    querydb(sql, params).then(data => {
        console.log("添加学生数据成功!");
        res.send({
            resultCode: 1,
            resultInfo: 'ok',
        });
    }).catch(error => {
        console.log(error);
        res.send({
            resultCode: -1,
            resultInfo: 'no',
        });
    });

})

/**
 * 学生列表
 */
router.get("/list", function (req, res) {
    var sql = "SELECT * FROM y_student";
    
    console.log("student list >>>> ");

    querydb(sql).then(function (data) {
        console.log(JSON.stringify(data));
        if (data != null && data.length > 0) {
            res.send({
                resultCode: 1,
                resultInfo: data,
            })
        } else {
            res.send({
                resultCode: -1,
                resultInfo: '没有数据',
            });
        }
    })
});

module.exports = router;